const fs = require('fs');
const path = require('path');

// 读取 view 目录，生成路由配置
export const generateRoutes = (viewDir) => {
    const routes = [];
    // 递归读取目录
    function readDirectory(dir) {
        const files = fs.readdirSync(dir);
        files.forEach(file => {
            const filePath = path.join(dir, file);
            const stat = fs.statSync(filePath);

            if (stat.isDirectory()) {
                // 如果是目录，递归读取
                readDirectory(filePath);
            } else {
                // 如果是文件，生成路由
                const ext = path.extname(file);
                if (['.js', '.jsx', '.ts', '.tsx'].includes(ext)) {
                    // 生成路由路径
                    const routePath = path.relative(viewDir, filePath)
                       .replace(ext, '')
                       .replace(/\\/g, '/')
                       .replace(/index$/, '');

                    // 生成路由配置
                    const route = {
                        path: `/${routePath}`,
                        component: filePath
                    };
                    routes.push(route);
                }
            }
        });
    }

    // 开始读取 view 目录
    readDirectory(viewDir);
    return routes;
}

// const viewDir = path.join(__dirname, 'view');
// const routes = generateRoutes(viewDir);